package com.augmentum.oes.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.augmentum.oes.dao.UserDao;
import com.augmentum.oes.exception.ParameterException;
import com.augmentum.oes.exception.ServiceException;
import com.augmentum.oes.model.User;
import com.augmentum.oes.service.UserService;
import com.augmentum.oes.util.StringUtil;

@Service
public class UserServiceImpl implements UserService {

    private static final String USER_NAME_MSG = "userNameMessage";
    private static final String USER_NAME_REQUIRED = "UserName is required!";
    private static final String PASSWORD_MSG = "passwordMessage";
    private static final String PASSWORD_REQUIRED = "Password is required!";
    private static final String USER_NAME_ERROR = "UserName Error!";
    private static final String PASSWORD_ERROR = "Password Error!";
    private static final String USER_ID_ERROR_MSG = "UserIdError";
    private static final String USER_ID_ERROR = "UserId Error!";
    private static final String PASSWORD_ERROR_MSG = "passwordError";
    private static final String USER_ERROR_MSG = "userError";
    private static final String USER_ERROR = "userError!";

    @Resource
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public UserServiceImpl() {
        super();
    }

    @Override
    public User login(String userName, String password) throws ParameterException, ServiceException {
        ParameterException parameterException = new ParameterException();
        if (StringUtil.isEmpty(userName)) {
            parameterException.addErrorMessage(USER_NAME_MSG, USER_NAME_REQUIRED);
        }
        if (StringUtil.isEmpty(password)) {
            parameterException.addErrorMessage(PASSWORD_MSG, PASSWORD_REQUIRED);
        }
        if (parameterException.isErrorMessage()) {
            throw parameterException;
        }

        User user = userDao.getByUserName(userName);
        if (user == null) {
            throw new ServiceException(USER_NAME_ERROR);
        }
        if (!user.getPassword().equals(password)) {
            throw new ServiceException(PASSWORD_ERROR);
        }
        return user;
    }

    @Override
    public User getById(int id) throws ParameterException {
        ParameterException parameterException = new ParameterException();
        if (id <= 0) {
            parameterException.addErrorMessage(USER_ID_ERROR_MSG, USER_ID_ERROR);
        }
        if (parameterException.isErrorMessage()) {
            throw parameterException;
        }
        return userDao.getById(id);
    }

    @Override
    public void changePassword(User user) throws ParameterException {
        ParameterException parameterException = new ParameterException();
        if (user.getId() <= 0) {
            parameterException.addErrorMessage(USER_ID_ERROR_MSG, USER_ID_ERROR);
        }
        if (StringUtil.isEmpty(user.getPassword())) {
            parameterException.addErrorMessage(PASSWORD_ERROR_MSG, PASSWORD_ERROR);
        }
        if (parameterException.isErrorMessage()) {
            throw parameterException;
        }
        userDao.update(user);
    }

    @Override
    public void update(User user) throws ParameterException {
        ParameterException parameterException = new ParameterException();
        if (user == null) {
            parameterException.addErrorMessage(USER_ERROR_MSG, USER_ERROR);
        }
        if (parameterException.isErrorMessage()) {
            throw parameterException;
        }
        userDao.update(user);
    }

}
